package com.shenfa.action;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.json.simple.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionSupport;
import com.shenfa.bean.ProductInfType;
import com.shenfa.service.ProductService;

import com.shenfa.util.ResultObject;
import com.shenfa.util.sequence.SequenceUtil;
import com.shenfa.bean.TreeNode;

public class ProductAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	// 返回结果集对象
	public JSONObject result;
	// 分页，第几条数据
	private int start;
	// 每页显示数据条数
	private int limit;
	// 接收当前操作的产品id
	private String idlist;
	// 接受产品集合条件
	private ProductInfType prodinty;
	
	private static Logger log=Logger.getLogger(ProductAction.class);

	// service
	@Autowired
	private ProductService productService;

	@Autowired
	private SequenceUtil sequenceUtil;
	
	/**
	 * 分页显示
	 * 根据上级产品id查询
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String GridList() {
		log.debug(prodinty.toString()+",start="+start+",limit="+limit);
		List<ProductInfType> list = productService.doGridList(prodinty, start,limit);
		result = ResultObject.build("0", list);
		result.put("total",productService.queryCount(prodinty));
		return SUCCESS;
	}

	/**
	 * 名称下拉选项
	 * @return
	 */
	public String ProdName() {
		List<ProductInfType> list = productService.doProdNameList();
		result = ResultObject.build("0", list);
		return SUCCESS;
	}

	/**
	 * 类型下拉选项
	 * 
	 * @author longchensan create by 2011-8-27
	 * @return
	 */
	public String ProdType() {
		log.debug("prodinty="+prodinty);
		List<TreeNode> list = productService.doTreeRoot(prodinty);
		result = ResultObject.build("0", list);
		return SUCCESS;
	}

	
	/**
	 * grid
	 * 
	 * @author longchensan create by 2011-8-27
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String queryProdsType() {
		log.debug("prodinty="+prodinty);
		List<ProductInfType> list = productService.queryProdsType(prodinty,start,limit);
		result = ResultObject.build("0", list);
		result.put("total",productService.TotalRulePage(prodinty));
		return SUCCESS;
	}
	
	/**
	 * 新增卡产品时,自动生成id序列号
	 * 
	 * @return
	 */
	public String doProdId(){
		List<ProductInfType> list =new ArrayList<ProductInfType>();
		prodinty = new ProductInfType();
		prodinty.setProdId(sequenceUtil.getSequence(3));
		list.add(prodinty);
		result = ResultObject.build("0", list);
		return SUCCESS;
	}
	
	/**
	 * 添加产品信息
	 * 
	 * @author longchensan create by 2011-8-27
	 * @return
	 */
	public String addProdInfo() {
		log.debug("prodinty="+prodinty+",idlist="+idlist);
		boolean res = productService.addProdInfo(prodinty, idlist);
		result = ResultObject.build("0", res);
		return SUCCESS;
	}

	/**
	 * 修改产品信息
	 * 
	 * @author longchensan create by 2011-8-27
	 * @return
	 */
	public String editProdInfo() {
		log.debug(prodinty.toString()+",idlist="+idlist);
		boolean res = productService.editProdInfo(prodinty,idlist);
		result = ResultObject.build("0", res);
		return SUCCESS;
	}

	/**
	 * 删除产品信息
	 * 
	 * @author longchensan create by 2011-8-27
	 * @return
	 */
	public String deleteProdInfo() {
		log.debug(prodinty.toString());
		boolean res = productService.deleteProdInfo(prodinty);
		result = ResultObject.build("0", res);
		return SUCCESS;
	}
	
	/**
	 * 添加卡类型信息
	 * 
	 * @author longchensan create by 2011-8-27
	 * @return
	 */
	public String addProdType() {
		log.debug(prodinty.toString());
		boolean res = productService.addProdType(prodinty);
		result = ResultObject.build("0", res);
		return SUCCESS;
	}

	/**
	 * 判断卡类型id是否有重复
	 * 
	 * @author longchensan create by 2011-8-27
	 * @return
	 */
	public String idYON() {
		log.debug(prodinty.toString());
		ProductInfType res = productService.queryIdYON(prodinty);
		result = ResultObject.build("0", res);
		return SUCCESS;
	}
	
	/**
	 * 删除卡类型时判断是否有字节点,有则禁止删除
	 * 
	 * @author longchensan create by 2011-10-14
	 * @return
	 */
	public String prodCount() {
		log.debug(prodinty.toString());
		int res = productService.queryProdForType(prodinty.getProdTypeId());
		result = ResultObject.build(""+res, res);
		return SUCCESS;
	}
	
	/**
	 * 修改卡类型信息
	 * 
	 * @author longchensan create by 2011-8-27
	 * @return
	 */
	public String editProdType() {
		log.debug(prodinty.toString());
		boolean res = productService.editProdType(prodinty);
		result = ResultObject.build("0", res);
		return SUCCESS;
	}

	/**
	 * 删除卡类型信息
	 * 
	 * @author longchensan create by 2011-8-27
	 * @return
	 */
	public String deleteProdType() {
		log.debug(prodinty.toString());
		boolean res = productService.deleteProdType(prodinty);
		result = ResultObject.build("0", res);
		return SUCCESS;
	}

	

	/**
	 * 分页处理，起始条数
	 * 
	 * @param start
	 */
	public void setStart(int start) {
		this.start = Math.abs(start);
	}

	/**
	 * 分页处理，每页数据条数
	 * 
	 * @param limit
	 */
	public void setLimit(int limit) {
		this.limit = Math.abs(limit);
	}

	/**
	 * 接收页面数据对象
	 * 
	 * @return
	 */
	public void setProdinty(ProductInfType prodinty) {
		this.prodinty = prodinty;
	}

	public ProductInfType getProdinty() {
		return prodinty;
	}

	/**
	 * 接收产品id
	 * 
	 * @param idlist
	 */
	public void setidlist(String idlist) {
		this.idlist = idlist;
	}

}
